package ${package.Controller};

import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.authz.annotation.RequiresRoles;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xy.framework.mreturn.MReturnObject;
import com.xy.framework.mreturn.MReturnUntil;
import com.xy.framework.pager.PageRequest;
import com.xy.framework.util.uuid.UUIDUtils;

<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>
	static Logger log = LoggerFactory.getLogger(${table.controllerName}.class);

	@Autowired
	private ${table.serviceName} ${table.serviceName?substring(1)?uncap_first};

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody MReturnObject listModule(${entity} ${entity?uncap_first}, HttpServletRequest request) {
		PageRequest pageRequest = newPageRequest(request);
		return new MReturnObject(MReturnObject.SUCCESS, ${table.serviceName?substring(1)?uncap_first}.findColumnsPageAll(pageRequest),
				${table.serviceName?substring(1)?uncap_first}.getCount(pageRequest));
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody MReturnObject save(${entity} ${entity?uncap_first}) {
		try {
			${table.serviceName?substring(1)?uncap_first}.validateEntity(${entity?uncap_first});
			${entity?uncap_first}.setId(UUIDUtils.UUID());
			${table.serviceName?substring(1)?uncap_first}.save(${entity?uncap_first});
		} catch (Exception e) {
			log.info("${entity}保存失败", e);
			return new MReturnObject(MReturnObject.SUCCESS, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS);
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody MReturnObject update(${entity} ${entity?uncap_first}) {
		try {
			${table.serviceName?substring(1)?uncap_first}.validateEntity(${entity?uncap_first});
			${table.serviceName?substring(1)?uncap_first}.update(${entity?uncap_first});
		} catch (Exception e) {
			log.info("${entity}修改失败", e);
			return new MReturnObject(MReturnObject.ERROR, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS);
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/del/{id}", method = RequestMethod.GET)
	public @ResponseBody MReturnObject del(@PathVariable String id) {
		try {
			${table.serviceName?substring(1)?uncap_first}.deleteById(id);
		} catch (Exception e) {
			log.info("${entity}删除失败", e);
			return new MReturnObject(MReturnObject.ERROR, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS);
	}
}
</#if>
